//
//  HLNetWorking.swift
//  SwiftDemo
//
//  Created by hulinhu on 2017/11/17.
//  Copyright © 2017年 hulinhu. All rights reserved.
//

//单例

import UIKit
import Alamofire

//格式: typealias 闭包名称 = (参数名称: 参数类型) -> 返回值类型
typealias completionCallback = (_ response : AnyObject) -> Void

// 枚举
enum NetWorkTypeStyles {
    case  NetWorkTypeGET
    case  NetWorkTypePOST
}

class HLNetWorking: NSObject {
    // 闭包 请求
     static let shareInstance : HLNetWorking = {
        let netManage = HLNetWorking()
        return  netManage
    }()

}

// MARK -- 网络请求
extension HLNetWorking{
    
    func requestNetWork(netWorkType:NetWorkTypeStyles,requqestUrl:String ,parameters:[String : Any]? = nil ,completionCallback:@escaping completionCallback ) {
        let headers: HTTPHeaders = [
            "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
            "Accept": "application/json"
        ]
        // 网络请求
        if netWorkType == .NetWorkTypeGET {
            
            Alamofire.request(requqestUrl, method:.get, parameters: parameters, encoding: URLEncoding.default,headers:headers).responseJSON(completionHandler: { (response) in
                // 判断 成功失败的返回
                guard let result = response.result.value else {
                    print(response.result.error as AnyObject)
                    return
                }
                completionCallback(result as AnyObject)
            })
        }else {
            Alamofire.request(requqestUrl, method:.post , parameters: parameters, encoding: URLEncoding.default,headers:headers).responseJSON(completionHandler: { (response) in
                // 判断 成功失败的返回
                guard let result = response.result.value else {
                    print(response.result.error as AnyObject)
                    return
                }
                completionCallback(result as AnyObject)
            })
        }
    }
    
}




